rename WebhooksController to WebRequestsController; refactor and support deprecated methods for now

Andrew Cantino 10 years ago
parent
commit
db0fbe4c93

+ 1 - 0
CHANGES.md

@@ -1,5 +1,6 @@
1 1
 # Changes
2 2
 
3
+* 0.4 (April 10, 2014) - WebHooksController has been renamed to WebRequestsController and all HTTP verbs are now accepted and passed through to Agents' #receive_web_request method. The new DataOutputAgent returns JSON or RSS feeds of incoming Events via external web request.
3 4
 * 0.31 (Jan 2, 2014)   - Agents now have an optional keep\_events\_for option that is propagated to created events' expires\_at field, and they update their events' expires\_at fields on change.
4 5
 * 0.3 (Jan 1, 2014)    - Remove symbolization of memory, options, and payloads; convert memory, options, and payloads to JSON from YAML.  Migration will perform conversion and adjust tables to be UTF-8.  Recommend making a DB backup before migrating.
5 6
 * 0.2 (Nov 6, 2013)    - PeakDetectorAgent now uses `window_duration_in_days` and `min_peak_spacing_in_days`.  Additionally, peaks trigger when the time series rises over the standard deviation multiple, not after it starts to fall.

+ 41 - 0
app/controllers/web_requests_controller.rb

@@ -0,0 +1,41 @@
1
+# This controller is designed to allow your Agents to receive cross-site Webhooks (POSTs), or to output data streams.
2
+# When a POST or GET is received, your Agent will have #receive_web_request called on itself with the incoming params,
3
+# method, and requested content-type.
4
+#
5
+# Requests are routed as follows:
6
+#   http://yourserver.com/users/:user_id/web_requests/:agent_id/:secret
7
+# where :user_id is a User's id, :agent_id is an Agent's id, and :secret is a token that should be user-specifiable in
8
+# an Agent that implements #receive_web_request. It is highly recommended that every Agent verify this token whenever
9
+# #receive_web_request is called. For example, one of your Agent's options could be :secret and you could compare this
10
+# value to params[:secret] whenever #receive_web_request is called on your Agent, rejecting invalid requests.
11
+#
12
+# Your Agent's #receive_web_request method should return an Array of json_or_string_response, status_code, and
13
+# optional mime type.  For example:
14
+#   [{status: "success"}, 200]
15
+# or
16
+#   ["not found", 404, 'text/plain']
17
+
18
+class WebRequestsController < ApplicationController
19
+  skip_before_filter :authenticate_user!
20
+
21
+  def handle_request
22
+    user = User.find_by_id(params[:user_id])
23
+    if user
24
+      agent = user.agents.find_by_id(params[:agent_id])
25
+      if agent
26
+        content, status, content_type = agent.trigger_web_request(params.except(:action, :controller, :agent_id, :user_id, :format), request.method_symbol.to_s, request.format.to_s)
27
+        if content.is_a?(String)
28
+          render :text => content, :status => status || 200, :content_type => content_type || 'text/plain'
29
+        elsif content.is_a?(Hash)
30
+          render :json => content, :status => status || 200
31
+        else
32
+          head(status || 200)
33
+        end
34
+      else
35
+        render :text => "agent not found", :status => 404
36
+      end
37
+    else
38
+      render :text => "user not found", :status => 404
39
+    end
40
+  end
41
+end

+ 0 - 39
app/controllers/webhooks_controller.rb

@@ -1,39 +0,0 @@
1
-# This controller is designed to allow your Agents to receive cross-site Webhooks (POSTs), or to output data streams.
2
-# When a POST or GET is received, your Agent will have #receive_webhook called on itself with the incoming params.
3
-#
4
-# To implement webhooks, make POSTs to the following URL:
5
-#   http://yourserver.com/users/:user_id/webhooks/:agent_id/:secret
6
-# where :user_id is your User's id, :agent_id is an Agent's id, and :secret is a token that should be
7
-# user-specifiable in your Agent.  It is highly recommended that you verify this token whenever #receive_webhook
8
-# is called.  For example, one of your Agent's options could be :secret and you could compare this value
9
-# to params[:secret] whenever #receive_webhook is called on your Agent, rejecting invalid requests.
10
-#
11
-# Your Agent's #receive_webhook method should return an Array of [json_or_string_response, status_code, optional mime type].  For example:
12
-#   [{status: "success"}, 200]
13
-# or
14
-#   ["not found", 404, 'text/plain']
15
-
16
-class WebhooksController < ApplicationController
17
-  skip_before_filter :authenticate_user!
18
-
19
-  def handle_request
20
-    user = User.find_by_id(params[:user_id])
21
-    if user
22
-      agent = user.agents.find_by_id(params[:agent_id])
23
-      if agent
24
-        content, status, content_type = agent.trigger_webhook(params.except(:action, :controller, :agent_id, :user_id, :format), request.method_symbol.to_s, request.format.to_s)
25
-        if content.is_a?(String)
26
-          render :text => content, :status => status || 200, :content_type => content_type || 'text/plain'
27
-        elsif content.is_a?(Hash)
28
-          render :json => content, :status => status || 200
29
-        else
30
-          head(status || 200)
31
-        end
32
-      else
33
-        render :text => "agent not found", :status => 404
34
-      end
35
-    else
36
-      render :text => "user not found", :status => 404
37
-    end
38
-  end
39
-end

+ 13 - 5
app/models/agent.rb

@@ -73,7 +73,7 @@ class Agent < ActiveRecord::Base
73 73
     # Implement me in your subclass of Agent.
74 74
   end
75 75
 
76
-  def receive_webhook(params, method, format)
76
+  def receive_web_request(params, method, format)
77 77
     # Implement me in your subclass of Agent.
78 78
     ["not implemented", 404]
79 79
   end
@@ -136,10 +136,18 @@ class Agent < ActiveRecord::Base
136 136
     message.gsub(/<([^>]+)>/) { Utils.value_at(payload, $1) || "??" }
137 137
   end
138 138
 
139
-  def trigger_webhook(params, method, format)
140
-    receive_webhook(params, method, format).tap do
141
-      self.last_webhook_at = Time.now
142
-      save!
139
+  def trigger_web_request(params, method, format)
140
+    if respond_to?(:receive_webhook)
141
+      Rails.logger.warn "DEPRECATED: The .receive_webhook method is deprecated, please switch your Agent to use .receive_web_request."
142
+      receive_webhook(params).tap do
143
+        self.last_web_request_at = Time.now
144
+        save!
145
+      end
146
+    else
147
+      receive_web_request(params, method, format).tap do
148
+        self.last_web_request_at = Time.now
149
+        save!
150
+      end
143 151
     end
144 152
   end
145 153
 

+ 2 - 2
app/models/agents/data_output_agent.rb

@@ -8,7 +8,7 @@ module Agents
8 8
 
9 9
         This Agent will output data at:
10 10
 
11
-        `https://#{ENV['DOMAIN']}/users/#{user.id}/webhooks/#{id || '<id>'}/:secret.xml`
11
+        `https://#{ENV['DOMAIN']}/users/#{user.id}/web_requests/#{id || '<id>'}/:secret.xml`
12 12
 
13 13
         where `:secret` is one of the allowed secrets specified in your options and the extension can be `xml` or `json`.
14 14
 
@@ -75,7 +75,7 @@ module Agents
75 75
       options['template']['description'].presence || "A feed of Events received by the '#{name}' Huginn Agent"
76 76
     end
77 77
 
78
-    def receive_webhook(params, method, format)
78
+    def receive_web_request(params, method, format)
79 79
       if options['secrets'].include?(params['secret'])
80 80
         items = received_events.order('id desc').limit(events_to_show).map do |event|
81 81
           interpolated = Utils.recursively_interpolate_jsonpaths(options['template']['item'], event.payload, :leading_dollarsign_is_jsonpath => true)

+ 2 - 2
app/models/agents/twilio_agent.rb

@@ -75,10 +75,10 @@ module Agents
75 75
     end
76 76
 
77 77
     def post_url(server_url,secret)
78
-      "#{server_url}/users/#{self.user.id}/webhooks/#{self.id}/#{secret}"
78
+      "#{server_url}/users/#{self.user.id}/web_requests/#{self.id}/#{secret}"
79 79
     end
80 80
 
81
-    def receive_webhook(params, method, format)
81
+    def receive_web_request(params, method, format)
82 82
       if memory['pending_calls'].has_key? params['secret']
83 83
         response = Twilio::TwiML::Response.new {|r| r.Say memory['pending_calls'][params['secret']], :voice => 'woman'}
84 84
         memory['pending_calls'].delete params['secret']

+ 5 - 3
app/models/agents/webhook_agent.rb

@@ -1,6 +1,7 @@
1 1
 module Agents
2 2
   class WebhookAgent < Agent
3 3
     cannot_be_scheduled!
4
+    cannot_receive_events!
4 5
 
5 6
     description  do
6 7
         <<-MD
@@ -8,7 +9,7 @@ module Agents
8 9
 
9 10
         In order to create events with this agent, make a POST request to:
10 11
         ```
11
-           https://#{ENV['DOMAIN']}/users/#{user.id}/webhooks/#{id || '<id>'}/:secret
12
+           https://#{ENV['DOMAIN']}/users/#{user.id}/web_requests/#{id || '<id>'}/:secret
12 13
         ``` where `:secret` is specified in your options.
13 14
 
14 15
         The
@@ -36,9 +37,10 @@ module Agents
36 37
         "payload_path" => "payload"}
37 38
     end
38 39
 
39
-    def receive_webhook(params, method, format)
40
+    def receive_web_request(params, method, format)
40 41
       secret = params.delete('secret')
41
-      return ["Not Authorized", 401] unless secret == options['secret'] && method == "post"
42
+      return ["Please use POST requests only", 401] unless method == "post"
43
+      return ["Not Authorized", 401] unless secret == options['secret']
42 44
 
43 45
       create_event(:payload => payload_for(params))
44 46
 

+ 1 - 1
app/views/agents/agent_views/data_output_agent/_show.html.erb

@@ -5,7 +5,7 @@
5 5
 
6 6
 <ul>
7 7
   <% @agent.options['secrets'].each do |secret| %>
8
-    <% url = lambda { |format| webhooks_url(:agent_id => @agent.id, :user_id => current_user.id, :secret => secret, :format => format) } %>
8
+    <% url = lambda { |format| web_requests_url(:agent_id => @agent.id, :user_id => current_user.id, :secret => secret, :format => format) } %>
9 9
     <li><%= link_to url.call(:json), url.call(:json), :target => :blank %></li>
10 10
     <li><%= link_to url.call(:xml), url.call(:xml), :target => :blank %></li>
11 11
   <% end %>

+ 3 - 0
app/views/agents/agent_views/webhook_agent/_show.html.erb

@@ -0,0 +1,3 @@
1
+<p>
2
+  Send WebHooks (POST requests) to this Agent at <%= link_to web_requests_url(:agent_id => @agent.id, :user_id => current_user.id, :secret => @agent.options['secret']), web_requests_url(:agent_id => @agent.id, :user_id => current_user.id, :secret => @agent.options['secret']), :target => :blank %>
3
+</p>

+ 3 - 1
config/routes.rb

@@ -31,7 +31,9 @@ Huginn::Application.routes.draw do
31 31
   match "/worker_status" => "worker_status#show"
32 32
 
33 33
   post "/users/:user_id/update_location/:secret" => "user_location_updates#create"
34
-  match "/users/:user_id/webhooks/:agent_id/:secret" => "webhooks#handle_request", :as => :webhooks
34
+
35
+  match "/users/:user_id/web_requests/:agent_id/:secret" => "web_requests#handle_request", :as => :web_requests
36
+  post "/users/:user_id/webhooks/:agent_id/:secret" => "web_requests#handle_request" # legacy
35 37
 
36 38
 #  match "/delayed_job" => DelayedJobWeb, :anchor => false
37 39
   devise_for :users, :sign_out_via => [ :post, :delete ]

+ 9 - 0
db/migrate/20140408150825_rename_webhook_to_web_request.rb

@@ -0,0 +1,9 @@
1
+class RenameWebhookToWebRequest < ActiveRecord::Migration
2
+  def up
3
+    rename_column :agents, :last_webhook_at, :last_web_request_at
4
+  end
5
+
6
+  def down
7
+    rename_column :agents, :last_web_request_at, :last_webhook_at
8
+  end
9
+end

+ 15 - 23
db/schema.rb

@@ -11,21 +11,21 @@
11 11
 #
12 12
 # It's strongly recommended to check this file into your version control system.
13 13
 
14
-ActiveRecord::Schema.define(:version => 20140216201250) do
14
+ActiveRecord::Schema.define(:version => 20140408150825) do
15 15
 
16 16
   create_table "agent_logs", :force => true do |t|
17
-    t.integer  "agent_id",                                             :null => false
18
-    t.text     "message",           :limit => 16777215,                :null => false
19
-    t.integer  "level",                                 :default => 3, :null => false
17
+    t.integer  "agent_id",                         :null => false
18
+    t.text     "message",                          :null => false
19
+    t.integer  "level",             :default => 3, :null => false
20 20
     t.integer  "inbound_event_id"
21 21
     t.integer  "outbound_event_id"
22
-    t.datetime "created_at",                                           :null => false
23
-    t.datetime "updated_at",                                           :null => false
22
+    t.datetime "created_at",                       :null => false
23
+    t.datetime "updated_at",                       :null => false
24 24
   end
25 25
 
26 26
   create_table "agents", :force => true do |t|
27 27
     t.integer  "user_id"
28
-    t.text     "options",               :limit => 16777215
28
+    t.text     "options"
29 29
     t.string   "type"
30 30
     t.string   "name"
31 31
     t.string   "schedule"
@@ -36,10 +36,10 @@ ActiveRecord::Schema.define(:version => 20140216201250) do
36 36
     t.datetime "created_at",                                                     :null => false
37 37
     t.datetime "updated_at",                                                     :null => false
38 38
     t.text     "memory",                :limit => 2147483647
39
-    t.datetime "last_webhook_at"
39
+    t.datetime "last_web_request_at"
40
+    t.integer  "keep_events_for",                             :default => 0,     :null => false
40 41
     t.datetime "last_event_at"
41 42
     t.datetime "last_error_log_at"
42
-    t.integer  "keep_events_for",                             :default => 0,     :null => false
43 43
     t.boolean  "propagate_immediately",                       :default => false, :null => false
44 44
   end
45 45
 
@@ -47,19 +47,11 @@ ActiveRecord::Schema.define(:version => 20140216201250) do
47 47
   add_index "agents", ["type"], :name => "index_agents_on_type"
48 48
   add_index "agents", ["user_id", "created_at"], :name => "index_agents_on_user_id_and_created_at"
49 49
 
50
-  create_table "contacts", :force => true do |t|
51
-    t.text     "message"
52
-    t.string   "name"
53
-    t.string   "email"
54
-    t.datetime "created_at", :null => false
55
-    t.datetime "updated_at", :null => false
56
-  end
57
-
58 50
   create_table "delayed_jobs", :force => true do |t|
59 51
     t.integer  "priority",                       :default => 0
60 52
     t.integer  "attempts",                       :default => 0
61 53
     t.text     "handler",    :limit => 16777215
62
-    t.text     "last_error", :limit => 16777215
54
+    t.text     "last_error"
63 55
     t.datetime "run_at"
64 56
     t.datetime "locked_at"
65 57
     t.datetime "failed_at"
@@ -74,11 +66,11 @@ ActiveRecord::Schema.define(:version => 20140216201250) do
74 66
   create_table "events", :force => true do |t|
75 67
     t.integer  "user_id"
76 68
     t.integer  "agent_id"
77
-    t.decimal  "lat",                              :precision => 15, :scale => 10
78
-    t.decimal  "lng",                              :precision => 15, :scale => 10
79
-    t.text     "payload",    :limit => 2147483647
80
-    t.datetime "created_at",                                                       :null => false
81
-    t.datetime "updated_at",                                                       :null => false
69
+    t.decimal  "lat",                            :precision => 15, :scale => 10
70
+    t.decimal  "lng",                            :precision => 15, :scale => 10
71
+    t.text     "payload",    :limit => 16777215
72
+    t.datetime "created_at",                                                     :null => false
73
+    t.datetime "updated_at",                                                     :null => false
82 74
     t.datetime "expires_at"
83 75
   end
84 76
 

+ 28 - 28
spec/controllers/webhooks_controller_spec.rb

@@ -1,15 +1,15 @@
1 1
 require 'spec_helper'
2 2
 
3
-describe WebhooksController do
4
-  class Agents::WebhookReceiverAgent < Agent
3
+describe WebRequestsController do
4
+  class Agents::WebRequestReceiverAgent < Agent
5 5
     cannot_receive_events!
6 6
     cannot_be_scheduled!
7 7
 
8
-    def receive_webhook(params, method, format)
8
+    def receive_web_request(params, method, format)
9 9
       if params.delete(:secret) == options[:secret]
10
-        memory[:webhook_values] = params
11
-        memory[:webhook_format] = format
12
-        memory[:webhook_method] = method
10
+        memory[:web_request_values] = params
11
+        memory[:web_request_format] = format
12
+        memory[:web_request_method] = method
13 13
         ["success", 200, memory['content_type']]
14 14
       else
15 15
         ["failure", 404]
@@ -18,32 +18,32 @@ describe WebhooksController do
18 18
   end
19 19
 
20 20
   before do
21
-    stub(Agents::WebhookReceiverAgent).valid_type?("Agents::WebhookReceiverAgent") { true }
22
-    @agent = Agents::WebhookReceiverAgent.new(:name => "something", :options => { :secret => "my_secret" })
21
+    stub(Agents::WebRequestReceiverAgent).valid_type?("Agents::WebRequestReceiverAgent") { true }
22
+    @agent = Agents::WebRequestReceiverAgent.new(:name => "something", :options => { :secret => "my_secret" })
23 23
     @agent.user = users(:bob)
24 24
     @agent.save!
25 25
   end
26 26
 
27
-  it "should not require login to trigger a webhook" do
28
-    @agent.last_webhook_at.should be_nil
27
+  it "should not require login to receive a web request" do
28
+    @agent.last_web_request_at.should be_nil
29 29
     post :handle_request, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"
30
-    @agent.reload.last_webhook_at.should be_within(2).of(Time.now)
30
+    @agent.reload.last_web_request_at.should be_within(2).of(Time.now)
31 31
     response.body.should == "success"
32 32
     response.should be_success
33 33
   end
34 34
 
35
-  it "should call receive_webhook" do
35
+  it "should call receive_web_request" do
36 36
     post :handle_request, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"
37 37
     @agent.reload
38
-    @agent.memory[:webhook_values].should == { 'key' => "value", 'another_key' => "5" }
39
-    @agent.memory[:webhook_format].should == "text/html"
40
-    @agent.memory[:webhook_method].should == "post"
38
+    @agent.memory[:web_request_values].should == { 'key' => "value", 'another_key' => "5" }
39
+    @agent.memory[:web_request_format].should == "text/html"
40
+    @agent.memory[:web_request_method].should == "post"
41 41
     response.body.should == "success"
42 42
     response.headers['Content-Type'].should == 'text/plain; charset=utf-8'
43 43
     response.should be_success
44 44
 
45 45
     post :handle_request, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "not_my_secret", :no => "go"
46
-    @agent.reload.memory[:webhook_values].should_not == { 'no' => "go" }
46
+    @agent.reload.memory[:web_request_values].should_not == { 'no' => "go" }
47 47
     response.body.should == "failure"
48 48
     response.should be_missing
49 49
   end
@@ -51,9 +51,9 @@ describe WebhooksController do
51 51
   it "should accept gets" do
52 52
     get :handle_request, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5"
53 53
     @agent.reload
54
-    @agent.memory[:webhook_values].should == { 'key' => "value", 'another_key' => "5" }
55
-    @agent.memory[:webhook_format].should == "text/html"
56
-    @agent.memory[:webhook_method].should == "get"
54
+    @agent.memory[:web_request_values].should == { 'key' => "value", 'another_key' => "5" }
55
+    @agent.memory[:web_request_format].should == "text/html"
56
+    @agent.memory[:web_request_method].should == "get"
57 57
     response.body.should == "success"
58 58
     response.should be_success
59 59
   end
@@ -61,21 +61,21 @@ describe WebhooksController do
61 61
   it "should pass through the received format" do
62 62
     get :handle_request, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5", :format => :json
63 63
     @agent.reload
64
-    @agent.memory[:webhook_values].should == { 'key' => "value", 'another_key' => "5" }
65
-    @agent.memory[:webhook_format].should == "application/json"
66
-    @agent.memory[:webhook_method].should == "get"
64
+    @agent.memory[:web_request_values].should == { 'key' => "value", 'another_key' => "5" }
65
+    @agent.memory[:web_request_format].should == "application/json"
66
+    @agent.memory[:web_request_method].should == "get"
67 67
 
68 68
     post :handle_request, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5", :format => :xml
69 69
     @agent.reload
70
-    @agent.memory[:webhook_values].should == { 'key' => "value", 'another_key' => "5" }
71
-    @agent.memory[:webhook_format].should == "application/xml"
72
-    @agent.memory[:webhook_method].should == "post"
70
+    @agent.memory[:web_request_values].should == { 'key' => "value", 'another_key' => "5" }
71
+    @agent.memory[:web_request_format].should == "application/xml"
72
+    @agent.memory[:web_request_method].should == "post"
73 73
 
74 74
     put :handle_request, :user_id => users(:bob).to_param, :agent_id => @agent.id, :secret => "my_secret", :key => "value", :another_key => "5", :format => :atom
75 75
     @agent.reload
76
-    @agent.memory[:webhook_values].should == { 'key' => "value", 'another_key' => "5" }
77
-    @agent.memory[:webhook_format].should == "application/atom+xml"
78
-    @agent.memory[:webhook_method].should == "put"
76
+    @agent.memory[:web_request_values].should == { 'key' => "value", 'another_key' => "5" }
77
+    @agent.memory[:web_request_format].should == "application/atom+xml"
78
+    @agent.memory[:web_request_method].should == "put"
79 79
   end
80 80
 
81 81
   it "can accept a content-type to return" do

+ 48 - 0
spec/models/agent_spec.rb

@@ -514,7 +514,55 @@ describe Agent do
514 514
         end
515 515
       end
516 516
     end
517
+  end
518
+
519
+  describe ".trigger_web_request" do
520
+    class Agents::WebRequestReceiver < Agent
521
+      cannot_be_scheduled!
522
+    end
523
+
524
+    before do
525
+      stub(Agents::WebRequestReceiver).valid_type?("Agents::WebRequestReceiver") { true }
526
+    end
527
+
528
+    context "when .receive_web_request is defined" do
529
+      before do
530
+        @agent = Agents::WebRequestReceiver.new(:name => "something")
531
+        @agent.user = users(:bob)
532
+        @agent.save!
533
+
534
+        def @agent.receive_web_request(params, method, format)
535
+          memory['last_request'] = [params, method, format]
536
+          ['Ok!', 200]
537
+        end
538
+      end
539
+
540
+      it "calls the .receive_web_request hook, updates last_web_request_at, and saves" do
541
+        @agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html")
542
+        @agent.reload.memory['last_request'].should == [ { "some_param" => "some_value" }, "post", "text/html" ]
543
+        @agent.last_web_request_at.to_i.should be_within(1).of(Time.now.to_i)
544
+      end
545
+    end
517 546
 
547
+    context "when .receive_webhook is defined" do
548
+      before do
549
+        @agent = Agents::WebRequestReceiver.new(:name => "something")
550
+        @agent.user = users(:bob)
551
+        @agent.save!
552
+
553
+        def @agent.receive_webhook(params)
554
+          memory['last_webhook_request'] = params
555
+          ['Ok!', 200]
556
+        end
557
+      end
558
+
559
+      it "outputs a deprecation warning and calls .receive_webhook with the params" do
560
+        mock(Rails.logger).warn("DEPRECATED: The .receive_webhook method is deprecated, please switch your Agent to use .receive_web_request.")
561
+        @agent.trigger_web_request({ :some_param => "some_value" }, "post", "text/html")
562
+        @agent.reload.memory['last_webhook_request'].should == { "some_param" => "some_value" }
563
+        @agent.last_web_request_at.to_i.should be_within(1).of(Time.now.to_i)
564
+      end
565
+    end
518 566
   end
519 567
 
520 568
   describe "recent_error_logs?" do

+ 6 - 6
spec/models/agents/data_output_agent_spec.rb

@@ -62,7 +62,7 @@ describe Agents::DataOutputAgent do
62 62
     end
63 63
   end
64 64
 
65
-  describe "#receive_webhook" do
65
+  describe "#receive_web_request" do
66 66
     before do
67 67
       current_time = Time.now
68 68
       stub(Time).now { current_time }
@@ -70,15 +70,15 @@ describe Agents::DataOutputAgent do
70 70
     end
71 71
 
72 72
     it "requires a valid secret" do
73
-      content, status, content_type = agent.receive_webhook({ 'secret' => 'fake' }, 'get', 'text/xml')
73
+      content, status, content_type = agent.receive_web_request({ 'secret' => 'fake' }, 'get', 'text/xml')
74 74
       status.should == 401
75 75
       content.should == "Not Authorized"
76 76
 
77
-      content, status, content_type = agent.receive_webhook({ 'secret' => 'fake' }, 'get', 'application/json')
77
+      content, status, content_type = agent.receive_web_request({ 'secret' => 'fake' }, 'get', 'application/json')
78 78
       status.should == 401
79 79
       content.should == { :error => "Not Authorized" }
80 80
 
81
-      content, status, content_type = agent.receive_webhook({ 'secret' => 'secret1' }, 'get', 'application/json')
81
+      content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'application/json')
82 82
       status.should == 200
83 83
     end
84 84
 
@@ -100,7 +100,7 @@ describe Agents::DataOutputAgent do
100 100
       end
101 101
 
102 102
       it "can output RSS" do
103
-        content, status, content_type = agent.receive_webhook({ 'secret' => 'secret1' }, 'get', 'text/xml')
103
+        content, status, content_type = agent.receive_web_request({ 'secret' => 'secret1' }, 'get', 'text/xml')
104 104
         status.should == 200
105 105
         content_type.should == 'text/xml'
106 106
         content.gsub(/\s+/, '').should == Utils.unindent(<<-XML).gsub(/\s+/, '')
@@ -137,7 +137,7 @@ describe Agents::DataOutputAgent do
137 137
       it "can output JSON" do
138 138
         agent.options['template']['item']['foo'] = "hi"
139 139
 
140
-        content, status, content_type = agent.receive_webhook({ 'secret' => 'secret2' }, 'get', 'application/json')
140
+        content, status, content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
141 141
         status.should == 200
142 142
 
143 143
         content.should == {

+ 5 - 5
spec/models/agents/webhook_agent_spec.rb

@@ -10,11 +10,11 @@ describe Agents::WebhookAgent do
10 10
   end
11 11
   let(:payload) { {'some' => 'info'} }
12 12
 
13
-  describe 'receive_webhook' do
13
+  describe 'receive_web_request' do
14 14
     it 'should create event if secret matches' do
15 15
       out = nil
16 16
       lambda {
17
-        out = agent.receive_webhook({ 'secret' => 'foobar', 'payload' => payload }, "post", "text/html")
17
+        out = agent.receive_web_request({ 'secret' => 'foobar', 'payload' => payload }, "post", "text/html")
18 18
       }.should change { Event.count }.by(1)
19 19
       out.should eq(['Event Created', 201])
20 20
       Event.last.payload.should eq(payload)
@@ -23,7 +23,7 @@ describe Agents::WebhookAgent do
23 23
     it 'should not create event if secrets dont match' do
24 24
       out = nil
25 25
       lambda {
26
-        out = agent.receive_webhook({ 'secret' => 'bazbat', 'payload' => payload }, "post", "text/html")
26
+        out = agent.receive_web_request({ 'secret' => 'bazbat', 'payload' => payload }, "post", "text/html")
27 27
       }.should change { Event.count }.by(0)
28 28
       out.should eq(['Not Authorized', 401])
29 29
     end
@@ -31,9 +31,9 @@ describe Agents::WebhookAgent do
31 31
     it "should only accept POSTs" do
32 32
       out = nil
33 33
       lambda {
34
-        out = agent.receive_webhook({ 'secret' => 'foobar', 'payload' => payload }, "get", "text/html")
34
+        out = agent.receive_web_request({ 'secret' => 'foobar', 'payload' => payload }, "get", "text/html")
35 35
       }.should change { Event.count }.by(0)
36
-      out.should eq(['Not Authorized', 401])
36
+      out.should eq(['Please use POST requests only', 401])
37 37
     end
38 38
   end
39 39
 end

+ 11 - 7
spec/routing/webhooks_controller_spec.rb

@@ -1,19 +1,23 @@
1 1
 require 'spec_helper'
2 2
 
3
-describe "routing for webhooks" do
3
+describe "routing for web requests" do
4 4
   it "routes to handle_request" do
5 5
     resulting_params = { :user_id => "6", :agent_id => "2", :secret => "foobar" }
6
-    get("/users/6/webhooks/2/foobar").should route_to("webhooks#handle_request", resulting_params)
7
-    post("/users/6/webhooks/2/foobar").should route_to("webhooks#handle_request", resulting_params)
8
-    put("/users/6/webhooks/2/foobar").should route_to("webhooks#handle_request", resulting_params)
9
-    delete("/users/6/webhooks/2/foobar").should route_to("webhooks#handle_request", resulting_params)
6
+    get("/users/6/web_requests/2/foobar").should route_to("web_requests#handle_request", resulting_params)
7
+    post("/users/6/web_requests/2/foobar").should route_to("web_requests#handle_request", resulting_params)
8
+    put("/users/6/web_requests/2/foobar").should route_to("web_requests#handle_request", resulting_params)
9
+    delete("/users/6/web_requests/2/foobar").should route_to("web_requests#handle_request", resulting_params)
10
+  end
11
+
12
+  it "supports the legacy /webhooks/ route" do
13
+    post("/users/6/webhooks/2/foobar").should route_to("web_requests#handle_request", :user_id => "6", :agent_id => "2", :secret => "foobar")
10 14
   end
11 15
 
12 16
   it "routes with format" do
13
-    get("/users/6/webhooks/2/foobar.json").should route_to("webhooks#handle_request",
17
+    get("/users/6/web_requests/2/foobar.json").should route_to("web_requests#handle_request",
14 18
                                                            { :user_id => "6", :agent_id => "2", :secret => "foobar", :format => "json" })
15 19
 
16
-    get("/users/6/webhooks/2/foobar.atom").should route_to("webhooks#handle_request",
20
+    get("/users/6/web_requests/2/foobar.atom").should route_to("web_requests#handle_request",
17 21
                                                            { :user_id => "6", :agent_id => "2", :secret => "foobar", :format => "atom" })
18 22
   end
19 23
 end